//
//  ViewController.m
//  myPhonebook
//
//  Created by liuchengming on 16/4/27.
//  Copyright © 2016年 liuchengming. All rights reserved.
//

#import "ViewController.h"
#include "Phonedata.h"

@interface ViewController ()
{
    NSArray * data;
    BOOL show;
    
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = @"我的通讯录";
    UITableView * tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableview.dataSource = self ;
    
    [self.view insertSubview:tableview aboveSubview:tableview];
    
    NSBundle * bunle = [NSBundle mainBundle];
    
    NSURL * url = [bunle URLForResource:@"联系人" withExtension:@"vcf"];
    NSString * text = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    //NSLog(@"%@",text);
    
    NSArray * lines = [text componentsSeparatedByString:@"\n"];
    // NSMutableSet * set =[NSMutableSet new];
    Phonedata * item ;
    NSMutableArray * d =[NSMutableArray new];
    NSMutableSet<NSString *> *set = [NSMutableSet new];
    for (NSString * line in lines)
    {
        if ([line hasPrefix:@"BEGIN"])
        {
            // NSlog(@"parse start");
            item = [Phonedata new];
        }else if ([line hasPrefix:@"END"])
        {
            // NSlog(@"parse end");
            [d addObject:item];
        }else if ([line hasPrefix:@"N;"])
        {
            // N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E4=BD=99;=E9=86=B4;;;
            NSArray *upperComponents = [line componentsSeparatedByString:@":"];
            NSArray *components = [[upperComponents objectAtIndex:1] componentsSeparatedByString:@";"];
            
            NSString * lastName = [components objectAtIndex:0];
            NSString * firstName = [components objectAtIndex:1];
            
            lastName = [lastName stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
            firstName = [firstName stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
            
            // NSlog(@"name %@%@",[lastName stringByRemovingPercentEncoding],[firstName stringByRemovingPercentEncoding]);
        }else if ([line hasPrefix:@"EMAIL;"])
        {
            NSArray *components = [line componentsSeparatedByString:@":"];
            NSString *emailAddress = [components objectAtIndex:1];
            // NSlog(@"emailAddress %@",emailAddress);
            
        }else if ([line hasPrefix:@"TEL;"])
        {
            NSArray *components = [line componentsSeparatedByString:@":"];
            NSString *phoneNumber = [components objectAtIndex:1];
            // NSlog(@"phoneNumber %@",phoneNumber);
            item.tel= [phoneNumber substringToIndex:11];
        }else if ([line hasPrefix:@"FN;"]){
            //FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E4=BD=99=E9=86=B4
            NSArray *upperComponents = [line componentsSeparatedByString:@":"];
            NSString *name = upperComponents[1];
            name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
            name = [name stringByRemovingPercentEncoding];
            // NSlog(@"FN:%@",name);
            item.name = name;
        }else if ([line hasPrefix:@"ORG;"]){
            NSArray *upperComponents = [line componentsSeparatedByString:@":"];
            NSString *name = upperComponents[1];
            name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
            name = [name stringByRemovingPercentEncoding];
            // NSlog(@"ORG:%@",name);
            item.tel= name ? : @"未知";
            [set addObject:item.tel];
        }
    }
    data = d;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return data.count;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    Phonedata * p = data[indexPath.row];
    cell.textLabel.text = p.name;
    cell.detailTextLabel.text = p.tel;
    
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
